package com.chenjinyan.back.controller;


import com.chenjinyan.back.service.BackUserService;
import com.chenjinyan.back.utils.ExceptionEnum;
import com.chenjinyan.back.utils.ExceptionHandle;
import com.chenjinyan.back.utils.ResultUtil;
import com.chenjinyan.back.vo.BackUser;
import com.chenjinyan.back.vo.LoginUser;
import com.chenjinyan.back.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author chenjinyan
 * @version 1.0
 */
@RestController
@RequestMapping("/back")
public class BackUserController {
    @Autowired
    BackUserService backUserService;
    @Autowired
    private ExceptionHandle exceptionHandle;

    @PostMapping("/signup")
    public Result insertUser(@Valid @RequestBody BackUser backUser, BindingResult bindingResult) {
        Result result = ResultUtil.success();
        try {
            //参数校验
            if (bindingResult.hasErrors()) {
                List<ObjectError> errorList = bindingResult.getAllErrors();
                for (ObjectError error : errorList) {
                    ExceptionEnum.PARAMETER_WRONG.setMsg(error.getDefaultMessage());
                    return ResultUtil.error(ExceptionEnum.PARAMETER_WRONG);
                }
            }
            //保存用户
            int i = backUserService.insertUser(backUser);
            if (i < 0) {
                result = ResultUtil.error(ExceptionEnum.USER_IS_FIND);
            }
        } catch (Exception e) {
            result = exceptionHandle.exceptionGet(e);
        }

        return result;
    }

    @GetMapping("/signloginacc/{loginAccount}")
    public Result checkByLoginAcc(@PathVariable("loginAccount") String loginAccount) {
        Result result = ResultUtil.success();
        try {
            int i = backUserService.checkByLoginAcc(loginAccount);
            if (i < 0) {
                result = ResultUtil.error(ExceptionEnum.LOGIN_ACCOUNT_FOUND);
            }
        } catch (Exception e) {
            result = exceptionHandle.exceptionGet(e);
        }
        return result;
    }

    @GetMapping("/signphone/{phone}")
    public Result checkByPhone(@PathVariable("phone") String phone) {
        Result result = ResultUtil.success();
        try {
            int i = backUserService.checkByPhone(phone);
            if (i < 0) {
                result = ResultUtil.error(ExceptionEnum.PHONE_IS_FUND);
            }
        } catch (Exception e) {
            result = exceptionHandle.exceptionGet(e);
        }
        return result;
    }

    @GetMapping("/signemail/{email:.+}")
    public Result checkByEmail(@PathVariable("email") String email) {
        Result result = ResultUtil.success();
        try {
            int i = backUserService.checkByEmail(email);
            if (i < 0) {
                result = ResultUtil.error(ExceptionEnum.EMAIL_IS_FUND);
            }
        } catch (Exception e) {
            result = exceptionHandle.exceptionGet(e);
        }
        return result;
    }

    @PostMapping("/login")
    public Result login(@Valid @RequestBody LoginUser loginUser, BindingResult bindingResult) {
        Result result;
        try {
            //参数校验
            if (bindingResult.hasErrors()) {
                List<ObjectError> errorList = bindingResult.getAllErrors();
                for (ObjectError error : errorList) {
                    ExceptionEnum.PARAMETER_WRONG.setMsg(error.getDefaultMessage());
                    return ResultUtil.error(ExceptionEnum.PARAMETER_WRONG);
                }
            }
            String userName = loginUser.getUsername();
            String password = loginUser.getPassword();
            //用户登陆
            int i = backUserService.findByUsernameAndPassword(userName, password);
            if (i < 0) {
                //用户名或密码错误
                result = ResultUtil.error(ExceptionEnum.USERNAME_PASSWORD_IS_WRONG);
            } else {
                result = ResultUtil.success();
            }
        } catch (Exception e) {
            result = exceptionHandle.exceptionGet(e);
        }

        return result;
    }

}
